package vip.xiaonuo.smzq.modular.kcl.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import vip.xiaonuo.smzq.modular.kcl.entity.Ks;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KsAutocompleteVo;

import java.util.List;

/**
 * 矿山基本信息 Mapper
 *
 * @author eomer
 * @since 2024-02-22
 */
@Mapper
public interface KsMapper extends MPJBaseMapper<Ks> {
//    /**
//     * 矿山基本信息目录树（未过滤暂不上表矿区）
//     * param xzqhDm 行政区划代码
//     * param nd 年度
//     */
//    @Select("SELECT kqbh from kcl_ks where xzqdm::VARCHAR like '%' || #{xzqhDm} || '%' and nd=#{nd}  group by kqbh")
//    List<String> getKqbh(@Param("xzqhDm") String xzqhDm,@Param("nd") Integer nd);
    /**
     * 矿山基本信息目录树（过滤暂不上表矿区）
     * param xzqhDm 行政区划代码
     * param nd 年度
     */
    @Select("SELECT kqbh from kcl_ks where sbbs!=3 and xzqdm::VARCHAR like  #{xzqhDm} || '%' and nd=#{nd}  group by kqbh")
    List<String> getKqbh(@Param("xzqhDm") String xzqhDm,@Param("nd") Integer nd);
    @Select("SELECT kqbh from kcl_ks where xzqdm::VARCHAR like  #{xzqhDm} || '%' and nd=#{nd} and xkzh=#{xkzh} group by kqbh")
    List<String> getKqbhKqr(@Param("xzqhDm") String xzqhDm,@Param("nd") Integer nd,@Param("xkzh") String xkzh);

    @Select("WITH\n" +
            " KsAutocomplete as (SELECT sbbs, nd,ksmc,ksbh,xkzh,xzqdm,kqbh,djflbh,concat_ws('_',ksmc,djflbh) as ksmcFlag from kcl_ks)\n" +
            " SELECT ksmc,ksbh,xkzh,xzqdm,kqbh,djflbh,ksmcFlag from KsAutocomplete  ${ew.customSqlSegment}")
    Page<KsAutocompleteVo> getKsAutocompleteList(IPage<Ks> page, @Param(Constants.WRAPPER) Wrapper<Ks> queryWrapper);

    /**
     * 矿山基本信息目录树（过滤暂不上表矿区）
     * param sql
     */
    @Select("with allxzq AS(select x.dm from kcl_cb08_xzqh x\n" +
            "            left join kcl_cb08_xzqh shi on x.fdm=shi.dm \n" +
            "            left JOIN kcl_cb08_xzqh sheng on shi.fdm=sheng.dm \n" +
            "            where x.dm::varchar like #{xzqhdm} || '%' \n" +
            "            ORDER BY x.dm)\n" +
            " SELECT k.kqbh  from kcl_ks k \n" +
            "            where sbbs<>3 and k.xzqdm in (select * from allxzq) \n" +
            "            and k.kqbh is not null and k.kqbh <>''\n" +
            "            ${sql}\n" +
            "            GROUP BY k.kqbh  \n" +
            "            ORDER BY k.kqbh  ")
    List<String> getKqbhList(@Param(value = "sql") String sql,@Param("xzqhdm") String xzqhdm);



    @Delete("-- 删除表 资源储量 \n" +
            "DELETE FROM kcl_zycl WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 质量 \n" +
            "DELETE FROM kcl_zl WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 综合回收\n" +
            "DELETE FROM kcl_zhhs WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 坐标 \n" +
            "DELETE FROM kcl_zb WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 压覆 \n" +
            "DELETE FROM kcl_yf WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 矿山经济 \n" +
            "DELETE FROM kcl_ksjj WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 矿山 \n" +
            "DELETE FROM kcl_ks WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 矿产品 \n" +
            "DELETE FROM kcl_kcp WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 采选环 \n" +
            "DELETE FROM kcl_cxh WHERE ${sql};\n" +
            "\n" +
            "-- 删除表 报告 \n" +
            "DELETE FROM kcl_bg WHERE ${sql};\n" +
            "--删除储量审批\n" +
            "DELETE FROM kcl_clsp WHERE ${clspSql};\n" +
            "--删除数据预警\n" +
            "DELETE FROM kcl_sjyj WHERE ${sql};")
    void delectKs(@Param(value = "sql") String sql,@Param(value = "clspSql") String clspSql);
}
